/**
 * 此处创建了一个不同于书本的例子
 * 在思想上，企图模拟java，c++等语言的面向对象编程思想
 * 只不过rust是明目张胆地不支持继承，所以不演示那个，只能演示:封装和多态
 * --
 *    所谓的多态只能通过trait来实现，trait是rust中类似于接口的存在
 */
 pub mod animals{
    
    pub trait Animal{
        fn eat(&self,food:&String);
        fn run(&self);
        fn sleep(&mut self);
        fn is_wakeup(&self)->bool;
        fn wakeup(&mut self);
     }

    pub struct Dog{
        name:String,
        state:bool
    }

    impl Dog{
        pub fn new(name:String)->Self{
            Dog{name,state:true}
        } 
    }

    impl Animal for Dog{
        fn eat(&self,food:&String){
            if !self.is_wakeup(){
                println!("{} 正在睡觉...请不要打扰我，等醒了再吃",self.name);
                return;
            }
            if  food=="骨头"{
                println!("{} 正在吃{}",self.name,food); 
            }
            else{
                println!("{} 不吃{}",self.name,food); 
            }
        }
        fn run(&self){
            if !self.is_wakeup(){
                println!("{} 正在睡觉...请不要打扰我，等醒了再跑",self.name);
                return;
            }
            println!("{} is running",self.name);
        }
        fn sleep(&mut self){
            println!("{} is sleeping",self.name);
            self.state=false;
        }
        fn is_wakeup(&self)->bool{
            self.state
        }
        fn wakeup(&mut self){
            println!("{} 起来！",self.name);   
            self.state=true;
        }
    }

    pub struct Cat{
        name:String,
        state:bool
    }

    impl Cat{
        pub fn new(name:String)->Self{
            Cat{name,state:true}
        } 
    }

    impl Animal for Cat{
        fn eat(&self,food:&String){
            if !self.is_wakeup(){
                println!("{} 正在睡觉...",self.name);
                return;
            }
            if food=="鱼"{
                println!("{} 正在吃{}",self.name,food);
            }else{
                println!("{} 不吃{}",self.name,food);
            }
        }
        fn run(&self){
            if !self.is_wakeup(){
                println!("{} 正在睡觉...请不要打扰我，等醒了再跑",self.name);
                return;
            }
            println!("{} is running",self.name);
        }
        fn sleep(&mut self){
            println!("{} is sleeping",self.name);
            self.state=false;
        }
        fn is_wakeup(&self)->bool{
            self.state 
        }
        fn wakeup(&mut self){
            println!("{} 起来！",self.name);   
            self.state=true;
        }
    }

 }